<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<link rel="stylesheet" type="text/css" href="../../../Themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="../../../Themes/icon.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/demo.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/common.css">
		<script type="text/javascript" src="../../../JS/jquery.min.js"></script>
		<script type="text/javascript" src="../../../JS/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="../../../JS/finance.js"></script>
		<script type="text/javascript" src="../../../JS/jquery.edatagrid.js"></script>
		<script type="text/javascript" src="../../../JS/common.js"></script>
		<script type="text/javascript" src="../../../JS/json2.js"></script>
		<script type="text/javascript" src="../../../JS/SHA-1.js"></script>
		<style type="text/css">

		</style>
	</head>

	<body style="padding: 0px;">
		<div id="toolbar" class="receipts_toolbar_style"></div>
		<div>
			<form id="ff" method="post">
				<table cellpadding="5" class="displaytable" style="text-align: left;width: 80%;margin: 0 auto;">
					<tr>
						<td width="20%">
							<h id="star">* </h>出库单编号
						</td>
						<td width="30%">
							<input class="easyui-textbox" id="input_ORDER_NO" name="ORDER_NO" style="width: 100%" type="text" data-options="prompt:'由系统自动生成，不需填写',disabled:'true'" />
						</td>
						<td width="20%">
							<h id="star">* </h>销售单编号
						</td>
						<td width="30%">
							<input class="easyui-textbox" id="input_SALESORDER_ID" name="SALESORDER_ID" style="width: 100%" type="text" data-options="disabled:'true'" />
						</td>
					</tr>
					<tr>
						<td width="20%">
							<h id="star">*</h>用户组
						</td>
						<td width="30%">
							<input class="easyui-combobox" id="input_CUSTOMER_TYPE" name="CUSTOMER_TYPE" style="width: 100%" type="text" data-options="disabled:'true'" />
						</td>
						<td width="20%">
							<h id="star">* </h>配送单位
						</td>
						<td width="30%">
							<input class="easyui-textbox" id="input_CUSTOMER_NAME" name="CUSTOMER_NAME" style="width: 100%" type="text" data-options="disabled:'true'" />
						</td>
					</tr>
					<tr>
						<td width="20%">
							<h id="star">* </h>配送时间
						</td>
						<td width="30%">
							<input class="easyui-datebox" id="input_DISTRIBUTION_DATE" name="DISTRIBUTION_DATE" style="width: 100%" type="text" data-options="disabled:'true'" />
						</td>
					</tr>
					<tr>

					</tr>
				</table>
				<table id="tt">

				</table>
				<div style="padding: 5px 0px;">
					<table>
						<tr>
							<th>人民币金额合计</th>
							<td>
								<input type="text" class="print_text_style" style="border: none;" name="number" />
							</td>
							<th>人民币金额合计(大写)</th>
							<td width="auto">
								<input type="text" class="print_text_style" style="border: none; width: 500px;" id="dx" />
								<div id="dx"></div>
							</td>
						</tr>
					</table>
					<div style="margin-left: 20%;" id="new_footer">

					</div>
				</div>
			</form>
		</div>
	</body>
	<script type="text/javascript">
		var auth = "admin;" + hex_sha1("admin1");
		var id = getID(); //保存获取的ID
		var updataData; //保存深度拷贝后的数据
		console.log(id);
		$(document).ready(function() {
			if(id != AddFlag && id != undefined) {
				currid = id;
				loadRemote();
			} else if(id == undefined) {
				currid = AddFlag;
			}
		});
		
		$('#input_CUSTOMER_TYPE').combobox({
			valueField: 'CUSTOMER_TYPE',
			textField: 'CUSTOMER_TYPE',
			panelHeight: 'auto',
			mode: 'remote',
			method: 'get',
			url: RemoteLinkAddress + "/logistics/customertype/listall.do",
		});
		$('#input_CUSTOMER_NAME').combobox({
			valueField: 'CUSTOMER_NAME',
			textField: 'CUSTOMER_NAME',
			panelWidth: 200,
			panelHeight: 'auto',
			mode: 'remote',
			method: 'get',
			onChange: function(newValue, oldValue) {
				if(newValue != "" && newValue != undefined) {
					var url = RemoteLinkAddress + "/logistics/customer/findbypinyin?PINYINFIELDNAME=" + newValue.trim();
					$(this).combobox("reload", url);
				}
			},
			onSelect: function(record) { //当选中数据后调用该function，并得到该combobox对应的valuefield和textfield，赋值给record
				$('#input_CUSTOMER_TYPE').textbox('setValue', record.CUSTOMER_TYPE);
			} //调用easyui的textbox的写入值方法，将record中name为CUSTOMER_ID的写入id为customer_id的textbox中
		});
		addFooter($("#new_footer"));
		var receipts_type = RECEIPT_SAVE_SYMBOL;
		receipts_type += RECEIPT_SEARCH_SYMBOL;
		receipts_type += RECEIPT_EXPORT_SYMBOL;
		content = AddReceiptsToolBar(receipts_type, "");
		$("#toolbar").append(content); /*在ID=toolbar的标记后面添加content中的内容*/

		var _detailToolbarData = AddDetailToolBar(2);
		GetTable();
		$('#tt').datagrid('hideColumn', 'STOREHOUSE_ID');

		function getRemoteData(_url) {
			$.ajax({
				type: "get",
				url: _url,
				dataType: 'JSON',
				success: function(result) {
					if(result != null) {
						updataData = deepCopy(result);
						console.log(updataData.rows);
						updataData.SALESORDER_ID = updataData.ORDER_NO;
						updataData.ORDER_NO = "";
						updataData.DISTRIBUTION_DATE = updataData.DISTRIBUTION_DATE.replace(/-/g, "/");
						getFormData(updataData);
						var _data = updataData.rows;
						_data = changeWarehouseoutAmount(_data);
						_data = toFixTwo(_data);
						$('#tt').datagrid('loadData', _data);
						//						verifyStatus(result.ISVERIFY, true);
						CalSum();
					}
				},
				error: function(result) {
					$.messager.alert("消息", "请求失败");
				}
			});
		}

		function loadRemote(_url) {
			var _url = RemoteLinkAddress + '/logistics/salesorder/findbyid.do?ID=' + id;
			getRemoteData(_url);
		}

		function save() {
			getSaveData();
		}

		function searchmore() {
			AddTabIn("新增选项", parent.$("#tabs"), "UI/LOGISTICS/WareHouseOutCreate/WareHouseOutSearch.html");
		}

		function GetTable() {
			$('#tt').edatagrid({
				height: 290,
				toolbar: _detailToolbarData,
				collapsible: true,
				singleSelect: true,
				fitColumns: true,
				rownumbers: true,
				autoSave: true,
				columns: [
					[{
							field: 'STOREHOUSE_ID',
							title: '库房ID',
							width: '13%',
							align: 'center'
						},
						{
							field: 'INTERNAL_NAME',
							title: '内部商品名',
							width: '13%',
							align: 'center'
						}, {
							field: 'INTERNAL_TYPE',
							title: '内部商品类型',
							width: '13%',
							align: 'center'
						},
						{
							field: 'STOREHOUSE_NAME',
							title: '库房',
							width: '10%',
							align: 'center'

						}, {
							field: 'UNIT_PRICE',
							title: '单价',
							width: '8%',
							align: 'center',
							formatter: function(value, row, index) {
								if(value != ''){
									return value + ' 元';
								}
							}
						},
						{
							field: 'STOCK',
							title: '库存量',
							width: '10%',
							align: 'center'
						},
						{
							field: 'AMOUNT',
							title: '销售数量',
							width: '10%',
							align: 'center'
						}, {
							field: 'UNIT_NAME',
							title: '原始单位',
							width: '8%',
							align: 'center'
						},
						{
							field: 'WAREHOUSEOUT_AMOUNT',
							title: '出库数量',
							width: '10%',
							align: 'center',
							editor: {
								type: 'numberbox'
							}
						}, {
							field: 'INOUT_UNIT',
							title:'单位',
							width:'5%',
							align:'center',
							editor: {
								type: 'textbox',
								options: {
									type: "text",
								}
							},
							formatter: function(value, row, index) {
								if(row.INOUT_UNIT === '' || row.INOUT_UNIT === undefined){
									row.INOUT_UNIT = row.UNIT_NAME
								}
								return row.INOUT_UNIT;
							}
						}, {
							field: 'WAREHOUSEOUT_SUM',
							title: '出库总价',
							width: '7%',
							align: 'center',
							editor: {
								type: 'numberbox',
								options: {
									required: true,
									precision: 2,
									groupSeparator: ',',
									decimalSeparator: '.',
									prefix: '￥'
								}
							},
							formatter: function(value, row, index) {
								if(value != ''){
									return value + ' 元';
								}
							}
						}
					]
				],
				onEndEdit: function(index, row, changes) {
					if(row.WAREHOUSEOUT_AMOUNT > row.STOCK) {
						$.messager.alert("警告", "出库数量不能大于库存量");
					}
					var result = row.UNIT_PRICE * row.WAREHOUSEOUT_AMOUNT;
					$('#tt').edatagrid('updateRow', {
						index: index,
						row: {
							WAREHOUSEOUT_SUM: result.toFixed(2)
						}
					});
					$('#tt').edatagrid('refreshRow', index);
					CalSum();
				}
			});
		}

		function getSaveData() {
			var _dataPage = submitFormData();
			_dataPage.SALESORDER_ID = updataData.ID;
			_dataPage.rows = updataData.rows;
			var m = _dataPage.rows;
			for(var i = 0; i < m.length; i++) {
				if(m[i].WAREHOUSEOUT_AMOUNT > m[i].STOCK) {
					$.messager.alert("警告", "第" + (i + 1) + "行出库数量不能大于库存量");
					return;
				}
			}
			var _data = JSON.stringify(_dataPage);
			submitData(_data);
		}

		function submitData(_data) {
			var _url = "";
			_url = RemoteLinkAddress + "/logistics/warehouseout/save.do";
			$.ajax({
				dataType: 'JSON',
				url: _url,
				data: 'a=' + _data,
				type: "POST",
				success: function(result) {
					if(result.msg.search("success") > -1) {
						var strArr = result.msg.split(";")
						$("#input_ORDER_NO").textbox("setValue", strArr[2]);
						currPage = strArr[1];
						$.messager.alert({
							title: '成功',
							msg: '保存成功',
							fn: function() {
								parent.$('#tabs').tabs('close', "新增出库单");
							}
						});
					} else {
						$.messager.alert("提示信息", result.msg);
					}
				},
				error: function(result) {
					$.messager.alert('提示信息', '新增失败');
				}
			});
		}

		function changeWarehouseoutAmount(data) {
			for(var s in data) {
				if(data[s].WAREHOUSEOUT_AMOUNT == "" || data[s].WAREHOUSEOUT_AMOUNT === undefined) {
					data[s].WAREHOUSEOUT_AMOUNT = data[s].AMOUNT;
				}
				data[s].WAREHOUSEOUT_SUM = data[s].WAREHOUSEOUT_AMOUNT * data[s].UNIT_PRICE;
			}
			return data;
		}

		function CalSum() {
			var _num = 0;
			var dataGridRows = $("#tt").datagrid("getRows");
			for(var i = 0; i < dataGridRows.length; i++) {
				_num += parseFloat(dataGridRows[i].WAREHOUSEOUT_SUM);
			}
			var num = changeMoneyToChinese(_num);
			$("#dx").val(num);
			$("input[name='number']").val(_num.toFixed(2));
		}

		function exportExcel() {
			if(currid === AddFlag) {
				$.messager.alert("警告", "该订单为新增订单，无法导出Excel！");
				return;
			}
			$.messager.confirm("提示", "确认导出Excel表格吗？", function(r) {
				if(r) {
					$.ajax({ //调用ajax方法，从服务器获取到该订单的所有数据
						type: "get",
						dataType: "json", //服务器返回的数据类型
						url: RemoteLinkAddress + "/logistics/warehouseout/excelorder.do",
						data: {
							"ID": _dataPage.ID
						},
						success: function(result) {
							window.location.href = result.path;
						}, //请求成功后，接受服务器传来的Json字符串，并做相应的处理
						error: function(xhr, status, error) {
							$.messager.alert("错误", "请求失败!");
						}
					});
				}
			});
		}

		function toFixTwo(data) {
			for(var singleData of data) {
				singleData.WAREHOUSEOUT_SUM = singleData.WAREHOUSEOUT_SUM.toFixed(2);
			}
			return data;
		}
	</script>

</html>